﻿
public class Solution189
{
    public void Rotate2(int[] nums, int k)
    {//一次移动k个，但是len可能小于k


        int[] temp = new int[k];
        int len = nums.Length;
        if (len == 1) return;


        for (int i = 0; i < k; i++)
        {

            temp[k - i - 1] = nums[len - 1 - i];

        }
        for (int i = 0; i < temp.Length; i++)
        {

            Console.Write(temp[i] + ",");
        }
        Console.WriteLine();
        for (int i = len - 1; i >= k; i--)
        {

            nums[i] = nums[i - k];



        }
        for (int i = 0; i < nums.Length; i++)
        {

            Console.Write(nums[i] + ",");
        }
        Console.WriteLine();
        for (int i = 0; i < k; i++)
        {

            nums[i] = temp[i];

        }



    }
    public void Rotate3(int[] nums, int k)
    {//一次移动1个
        int len = nums.Length;


        for (int i = 0; i < k; i++)
        {


            int temp = nums[len - 1];
            for (int j = len - 1; j > 0; j--)
            {
                nums[j] = nums[j - 1];

            }
            nums[0] = temp;


        }


    }
    public void Rotate5(int[] nums, int k)
    {//使用额外数组
        int len = nums.Length;

        int[] tempNums = new int[len];


        for (int i = 0; i < len; i++)
        {


            int nextIndex = (i + k) % len;
            tempNums[nextIndex] = nums[i];


        }

        for (int i = 0; i < len; i++)
        {



            nums[i] = tempNums[i];


        }

    }
    //环状替换
    public void Rotate6(int[] nums, int k)
    {
        int len = nums.Length;

        //循环次数count，每次走k格子，一共是k*count
        //也走了a圈，a*len=k*count,求count
        //a*len是lcm(n,k)
        //count=a*len/k=lcm(n,k)/k

        int count = 0;
        int i = 0;
        while (count <len)//循环几次可以遍历
        {

            

            //Console.WriteLine(i);
            int pre = nums[i];
            int start = i;
            do
            {
                int nextIndex = (start + k) % len;
                count++;

                int temp = nums[nextIndex];//记录现在的数
                nums[nextIndex] = pre;//前面的数覆盖过来
                pre = temp;//更新现在的数

                start = nextIndex;
               
            } while (start != i);


            i++;
        }





    }

    //两次反转
    public void Rotate(int[] nums, int k)
    {
        int len = nums.Length;

        //如果k大于len
        k= k  % len;

        //轮转len次相当于不变

        Reverse(nums, 0, len-1);
        Reverse(nums, 0, k-1);
        Reverse(nums, k, len - 1);
    }

    private void Reverse(int[] nums, int start, int end)
    {

        while (start < end)
        {

            int temp= nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;


        }



        
    }



}
class Program189
{

    static void Main189(string[] args)
    {
        int[] nums = [1, 2, 3, 4, 5, 6, 7];
        int k = 3;

        Solution189 solution = new Solution189();
        solution.Rotate(nums, k);

        for (int i = 0; i < nums.Length; i++)
        {

            Console.WriteLine(nums[i]);
        }
    }
}